package cn.xkai.exercise.a;

import com.google.gson.Gson;

/**
 * @description: 旋转矩阵
 * 自己的思路：未完成
 * 借鉴的思路：对角线交换+水平交换，顺时针：先对角线后水平，逆时针：先水平后对角线
 * @author: kaixiang
 * @date: 2022-06-21
 **/
public class Solution5 {
    public void rotate(int[][] matrix) {
        int len = matrix.length;
        // 1  2  3
        // 4  5  6
        // 7  8  9
        // 对角线交换
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < i; ++j) {
                matrix[i][j] = matrix[i][j] ^ matrix[j][i];
                matrix[j][i] = matrix[i][j] ^ matrix[j][i];
                matrix[i][j] = matrix[i][j] ^ matrix[j][i];
            }
        }
        // 1  4  7
        // 2  5  8
        // 3  6  9
        // 水平交换
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len / 2; j++) {
                matrix[i][j] = matrix[i][j] ^ matrix[i][len - j - 1];
                matrix[i][len - j - 1] = matrix[i][j] ^ matrix[i][len - j - 1];
                matrix[i][j] = matrix[i][j] ^ matrix[i][len - j - 1];
            }
        }
        // 7  4  1
        // 8  5  2
        // 9  6  3
    }

    public static void main(String[] args) {
        int[] a1 = {1, 2, 3};
        int[] a2 = {4, 5, 6};
        int[] a3 = {7, 8, 9};
        int[][] a = {a1, a2, a3};
        Solution5 solution5 = new Solution5();
        solution5.rotate(a);
        System.out.println(new Gson().toJson(a));
    }
}
